﻿@using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant
@using CleanArchitecture.Blazor.Application.Features.Identity.DTOs
@using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs
@inherits MudComponentBase
@inject IStringLocalizer<Roles> L

<MudDialog>
    <DialogContent>
        <MudForm Model="Model" @ref="_form">
            <MudGrid>
                <MudItem sm="6" xs="12">
                    <MultiTenantAutocomplete @bind-Value="Model.Tenant"
                                             T="TenantDto"
                                             Label="@L["Tenant"]"
                                             Required="true"
                                             Validation="@(new Func<string, IEnumerable<string>>(ValidateTenant))"
                                             For="@(() => Model.Tenant)" />
                </MudItem>
                <MudItem sm="6" xs="12">
                    <MudTextField Label="@L[Model.GetMemberDescription(x => x.Name)]" 
                                 @bind-Value="Model.Name"
                                 Required="true"
                                 For="@(() => Model.Name)"
                                 Validation="@(new Func<string, IEnumerable<string>>(ValidateName))"
                                 RequiredError="@L["Role name is required!"]"
                                 MaxLength="50"
                                 Immediate="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12">
                    <MudTextField Label="@L[Model.GetMemberDescription(x => x.Description)]"
                                 For="@(() => Model.Description)"
                                 MaxLength="255"
                                 Placeholder="@L["Enter description for this role"]"
                                 @bind-Value="Model.Description">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel" >@ConstantString.Cancel</MudButton>
        <MudButton OnClick="Submit" Disabled="@_processing">
            @ConstantString.Save
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = default!;

    [EditorRequired] [Parameter] public ApplicationRoleDto Model { get; set; } = default!;

    private MudForm? _form;
    private bool _processing;

    private async Task Submit()
    {
        _processing = true;
        
        try
        {
            await _form!.Validate();
            if (_form!.IsValid)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        finally
        {
            _processing = false;
        }
    }

    private void Cancel()
    {
        MudDialog.Cancel();
    }

    private IEnumerable<string> ValidateTenant(string tenantId)
    {
        if (string.IsNullOrWhiteSpace(tenantId))
            yield return L["Tenant selection is required"];
    }

    private IEnumerable<string> ValidateName(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
            yield return L["Role name is required"];
        else if (name.Length < 2)
            yield return L["Role name must be at least 2 characters"];
        else if (name.Length > 50)
            yield return L["Role name must not exceed 50 characters"];
    }
}